<?php
    
    class DBConnection{

	private $m_mysqli = NULL;
	
	public function Connect() {
      $this->m_mysqli = new mysqli(DBSettings::DBHOST,
      							   DBSettings::DBUSER,
      							   DBSettings::DBPASSWORD,
      							   DBSettings::DATABASE);
          
          //Note charset should be set before any queries...
          $this->m_mysqli->set_charset("utf8");
      
      // check connection
      if ($this->m_mysqli->connect_errno) {
          Log::LogError("Connect failed: $this->m_mysqli->connect_error");
          return false;
      }
      return true;
    }
	
	public function Close() {
        $this->m_mysqli->close();
                $this->m_mysqli = NULL;
    }
	
	public function GetSingleLineAsAssociativeArray(mysqli_stmt $stmt) {
		                
                $result = $stmt->execute();
                $ret = array();
                $result = $stmt->get_result();
				
				$ret = $result->fetch_assoc();
				
            	$stmt->close();
                
                return $ret;
	}
	
	public function Prepare($sql) {
                if ($this->m_mysqli == NULL) {
                        Log::LogError("Unable to connect to the database");
						return false;
                }
                
                $stmt = $this->m_mysqli->prepare($sql);
                if ($stmt === false) {
                        Log::LogMessage("prepare of '$sql' failed " . $this->m_mysqli->error);
                        return false;
                }
                
                return $stmt;
    }
	

} 